#ifndef _SHADOW_SOCKET_COMMUCATION_FRAMEWORK_
#define _SHADOW_SOCKET_COMMUCATION_FRAMEWORK_

#include <pthread.h>

#include <map>
#include <list>

#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>

#include "SSocket.h"


#define DEFAULT_CHECK_SOCKET_TIMEOUT_INTERVAL 1

#define DEFAULT_EPOLL_INTERVAL 1

#define EPOLL_DEFAULT_SIZE 1000

using std::map;
using std::list;

typedef int (*SSCF_READ_EVENT_HANDLER)(SSocket *, void * arg);

class SSCF {

	public:
		SSCF();
		~SSCF();
		int init(int listen_port, int max_thread, int socket_idel_timeout, \
				SSCF_READ_EVENT_HANDLER read_event_handler, void * read_event_handler_arg);
		int start_svc();

		int get_socket_idel_timeout() { return _socket_idel_timeout; }
	protected:
		int start_listen_thread();
		int start_check_socket_timeout_thread();
		int start_main_svc_thread();
		int add_socket_to_svc_sockets(SSocket*);

		static void * listen_thread_routine(void *);
		static void * check_socket_timeout_thread_routine(void *);
		static void * main_svc_thread_routine(void *);
	private:
		int _socket_idel_timeout;
		SSCF_READ_EVENT_HANDLER _read_evnet_handler;
		void * _read_evnet_handler_arg;
		SSocket _listen_socket;
		map<int, SSocket *> _sockets;
		pthread_mutex_t _mutex;
		int _listen_port;
		int _max_thread_num;
		pthread_t _listen_thread;
		pthread_t _check_socket_timeout_thread;
		list<pthread_t> _main_svc_threads;
		log4cxx::LoggerPtr _logger;
};


#endif
